<?php

namespace App\Http\Controllers\Api\V1;

use App\B2BProject\Area;
use App\B2BProject\Custom\Business\Goods;
use App\B2BProject\Custom\Business\GoodsCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    /**
     * 获取商品列表
     *
     * @param Request $request
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function index(Request $request)
    {
        // 创建商品查询对象，首要条件是商品必须可视
        $goodsQueryObject = Goods::display();

        if ($categoryId = $request->query('category_id')) {
            $goodsQueryObject->where('category_id', $categoryId);
        }

        if ($classId = $request->query('class_id')) {

            $categoryObject = GoodsCategory::findOrFail($classId);

            if ($categoryObject->parent_id == 0) {
                $ids   = $categoryObject->children()->lists('id');
                $ids[] = $categoryObject->id;

                $goodsQueryObject->whereIn('class_id', $ids);
            } else {
                $goodsQueryObject->where('class_id', $classId);
            }
        }

        if ($brand = $request->query('brand_id')) {
            $goodsQueryObject->where('brand_id', $brand);
        }

        if ($store = $request->query('store_id')) {
            $goodsQueryObject->where('store_id', $store);
        }

        // 获取供采分销商品？
        if ($type = $request->query('grant')) {
            $goodsQueryObject->where('grant_status', $type);
        }

        if ($name = $request->query('name')) {
            $goodsQueryObject->where('name', 'like', "%$name%");
        }

        if ($sortMethod = $request->query('sort_method')) {
            if ($sortMethod == -1) {
                $sortMethod = 'asc';
            } else {
                $sortMethod = 'desc';
            }
        } else {
            $sortMethod = 'desc';
        }

        if ($sort = $request->query('sort')) {
            switch ($sort) {
                case 1: // 价格排序
                    $goodsQueryObject->orderBy('original_price', $sortMethod);
                    break;
                case 2: // 销量排序
                    // TODO 目前按总销量排序
                    $goodsQueryObject->orderBy('sell_count', $sortMethod);
                    break;
                case 3: // 评价数排序
                    $goodsQueryObject->orderBy('comment_count', $sortMethod);
                    break;
            }
        }

        // 获取数据集
        // TODO 后期会添加分页相关
        $collection = $goodsQueryObject->orderBy('created_at', 'desc')->get();

        $data = [];

        foreach ($collection as $goods) {
            $data[] = [
                'id'            => $goods->id,
                'name'          => $goods->name,
                'price'         => call_user_func(function (Goods $goods) {

                    if ($goods->isGranted()) {
                        return $goods->getActualPrice();
                    }

                    if (\Auth::guest()) {
                        return -2; // 需要登录
                    }

                    return -1; // 需要授权

                }, $goods),
                'class_id'      => $goods->class_id,
                'category_id'   => $goods->category_id,
                'minimum_order' => $goods->minimum_order,
                'unit'          => $goods->unit,
                'sell_count'    => $goods->sell_count,
                'comment_count' => $goods->comment_count,
                'stock'         => $goods->stock,
                'province_id'   => $goods->province_id,
                'city_id'       => $goods->city_id,
                'county_id'     => $goods->county_id,
                'province'      => Area::getName($goods->province_id),
                'city'          => Area::getName($goods->city_id),
                'county'        => Area::getName($goods->county_id),
                'provider'      => $goods->store->member->information->company,
                'cover'         => url($goods->cover()),
                'store_id'      => $goods->store_id,
                'brand_id'      => $goods->brand_id,
            ];
        }

        return success_return(['list' => $data]);
    }

    /**
     * 获取商品 详细信息
     *
     * @param $id
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function show($id)
    {
        $goods = Goods::where('id', $id)->first();

        $data = [
            'id'            => $goods->id,
            'name'          => $goods->name,
            'price'         => call_user_func(function (Goods $goods) {

                if ($goods->isGranted()) {
                    return $goods->getActualPrice();
                }

                if (\Auth::guest()) {
                    return -2; // 需要登录
                }

                return -1; // 需要授权

            }, $goods),
            'store_id'      => $goods->store_id,
            'brand_id'      => $goods->brand_id,
            'class_id'      => $goods->class_id,
            'category_id'   => $goods->category_id,
            'minimum_order' => $goods->minimum_order,
            'unit'          => $goods->unit,
            'sell_count'    => $goods->sell_count,
            'stock'         => $goods->stock,
            'comment_count' => $goods->comment_count, //总评论数
            'province_id'   => $goods->province_id,
            'city_id'       => $goods->city_id,
            'county_id'     => $goods->county_id,
            'province'      => Area::getName($goods->province_id),
            'city'          => Area::getName($goods->city_id),
            'county'        => Area::getName($goods->county_id),
            'provider'      => $goods->store->member->information->company,
            'authorize'     => $goods->isGranted(),
            'image'         => $goods->goodsImage,
            'detail'        => strip_tags($goods->detail), //商品详细描述
        ];
        return success_return(['list' => $data]);
    }
}
